/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ipark.sistemski;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Damjan Vantur
 */
public class ADataFile {
    private File file = new File("ADF.txt");
    private String times[] = new String[6]; //casi open-close
    private boolean status;
    private double dailyPrice; //cene
    private double onlinePrice;
    
    
       public ADataFile(){
         for(int i=0; i<6; i++)
           times[i]="";
          status = false;
          dailyPrice = 0.0;
          onlinePrice = 0.0;
            
         if(file.exists()==false){
            try {
                file.createNewFile();
                toFile();
            } catch (IOException ex) {
                Logger.getLogger(ADataFile.class.getName()).log(Level.SEVERE, null, ex);
            }

         }else  fromFile();
    }
    
    public String getOpenWeek(){ return times[0]; }
    public String getCloseWeek(){ return times[1]; }
    public String getOpenSat(){ return times[2]; }
    public String getCloseSat(){ return times[3]; }
    public String getOpenSun(){ return times[4]; }
    public String getCloseSun(){ return times[5]; }
    public double getDailyPrice(){ return dailyPrice; }
    public double getOnlinePrice(){ return onlinePrice; }
    public boolean getStatus(){ return status; }
    public void setOpenWeek(String a){
        if( !(times[0].equals(a)) ){
            times[0]=a;
            toFile(); 
        } 
    }
    public void setCloseWeek(String b){
     if( !(times[1].equals(b)) ){
            times[1]=b;
            toFile(); 
        } 
    }
    public void setOpenSat(String c){ 
     if( !(times[2].equals(c)) ){
            times[2]=c;
            toFile(); 
        } 
    
    }
    public void setCloseSat(String d){ 
     if( !(times[3].equals(d)) ){
            times[3]=d;
            toFile(); 
        } 
    }
    public void setOpenSun(String e){ 
     if( !(times[4].equals(e)) ){
            times[4]=e;
            toFile(); 
        } 
    }
    public void setCloseSun(String f){ 
     if( !(times[5].equals(f)) ){
            times[5]=f;
            toFile(); 
        } 
    }
    public void setDailyPrice(double g){ dailyPrice = g; toFile(); }
    public void setOnlinePrice(double h){ onlinePrice = h; toFile(); }
    public void setStatus(boolean z){ status = z; toFile(); }  

 
    public void fromFile(){
       String tmp;
          BufferedReader  br;
        try {
            br = new BufferedReader(new FileReader(file));
            for(int j=0; j<6; j++){
                tmp=br.readLine();
                times[j] = tmp; //time in format "hh:mm:ss"
            }
            tmp=br.readLine();
           dailyPrice = Double.parseDouble(tmp);
            tmp=br.readLine();
           onlinePrice = Double.parseDouble(tmp);
            tmp = br.readLine();
            status = Boolean.parseBoolean(tmp);
            br.close();
            
    }   catch (FileNotFoundException ex) {
            Logger.getLogger(ADataFile.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ADataFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void toFile(){

      try {
          if(file.exists()==false)
            file.createNewFile();
       BufferedWriter bw=new BufferedWriter(new FileWriter("ADF.txt")); 
         for(int j=0; j<6; j++){
             bw.write(times[j]); //time in format "hh:mm:ss"
                 bw.newLine();
                
            }
            bw.write(Double.toString(dailyPrice)); //stv vseh napak
            bw.newLine();
            bw.write(Double.toString(onlinePrice)); // dnevni dohodek
            bw.newLine();
            bw.write(Boolean.toString(status));
            bw.flush();
            bw.close();
            
        } catch (IOException ex) {
            Logger.getLogger(ADataFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }   
 }
